# 描述:
#       从数据集中找到最大值。
#       通过增加下标遍历数组，检测到零结束。

# 变量: 使用以下寄存器:
#       %edi - 保存当前数的下标
#       %ebx - 保存最大值
#       %eax - 当前值

# 数据段:
# data_items - 保存的数组

.section .rodata

data_items:
    .long 3,67,34,222,45,75,54,34,44,33,22,11,66,0

.section .text

msg:
    .string "output: %d\n"

.globl main

main:
    movl    $0, %edi
    movl    data_items(, %edi, 4), %eax  # %eax = data_items[%edi*4]
    movl    %eax, %ebx

start_loop:
    cmpl    $0, %eax
    je      loop_exit       # 如果相等
    incl    %edi            # %edi += 1
    movl    data_items(, %edi, 4), %eax
    cmpl    %ebx, %eax
    jle     start_loop
    movl    %eax, %ebx
    jmp     start_loop

loop_exit:
    # printf("output: %d\n")
    movl    $0, %eax        # 必须将eax置零才能成功 call printf ?
    movl    $msg, %edi
    movl    %ebx, %esi
    call    printf

    # exit(0)
    movl    $0, %edi    
    call    exit


# to run this file, follow the commands below
# $ gcc -no-pie maximum.s
# $ ./a.out
# output: 222
